package com.api.utils;


import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private int code;
    private int errno;

    /**
     * msg信息
     */
    private String msg;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 返回时间
     */
    private String date;



    public Result() {
        this.code = 200;
        this.msg = "操作成功";
        this.date = dateString();
    }

    public static Result err() {
        return err(1, "操作失败");
    }

    public static Result err(String msg) {
        return err(500, msg);
    }

    public static Result err(int code, String msg) {
        Result r = new Result();
        r.setCode(code);
        r.setMsg(msg);
        r.setDate(dateStaticString());
        return r;
    }

    public static Result ok(String msg) {
        Result r = new Result();
        r.setMsg(msg);
        return r;
    }

    public static Result ok(int code,Object obj) {
        Result r = new Result();
        r.setCode(code);
        r.setData(obj);
        return r;
    }

    public static Result ok(String msg,Object obj) {
        Result r = new Result();
        r.setMsg(msg);
        r.setData(obj);
        return r;
    }

    public static Result ok(Object obj) {
        Result r = new Result();
        r.setData(obj);
        return r;
    }

    public static Result ok() {
        return new Result();
    }

    public static Result notRight(){
        Result r = new Result();
        r.setCode(403);
        r.setMsg("暂无数据权限, 请联系管理员");
        r.setDate(dateStaticString());
        return r;
    }


    public static Result notLogin(String msg){
        return err(msg);
    }

    public static Result imgErr(String msg) {
        Result r = new Result();
        r.setErrno(1);
        r.setCode(500);
        r.setMsg(msg);
        r.setDate(dateStaticString());
        return r;
    }

    public static Result imgSuccess(Object obj) {
        Result r = new Result();
        r.setErrno(0);
        r.setData(obj);
        return r;
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public int getErrno() {
        return errno;
    }

    public void setErrno(int errno) {
        this.errno = errno;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getDate() {
        return dateStaticString();
    }

    public void setDate(String date) {
        this.date = date;
    }

    private String dateString() {
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    }
    public static String dateStaticString() {
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    }

    public static Result expError() {
        Result r = new Result();
        r.setErrno(1);
        r.setCode(-1);
        r.setMsg("已超过体验时间，如需开通正式版本，请联系开发者");
        r.setDate(dateStaticString());
        return r;
    }
}

